package com.wen.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.yygh.cmn.client.DictFeignClient;
import com.wen.yygh.enums.DictEnum;
import com.wen.yygh.model.user.Patient;
import com.wen.yygh.user.mapper.PatientMapper;
import com.wen.yygh.user.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Wenjl
 */
@Service
@Slf4j
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllByUserId(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<Patient> patients = baseMapper.selectList(queryWrapper);
        patients.forEach(item -> {
            this.packPatient(item);
        });
        log.info("patients=>" + patients);
        return patients;
    }

    @Override
    public Patient getPatientById(Long id) {
        //调用方法封装一下
        return this.packPatient(baseMapper.selectById(id));
    }

    private Patient packPatient(Patient patient) {
        //就诊人证件类型
        String certificatesTypeStr = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),
                patient.getCertificatesType());
        //就诊人联系人证件类型

        String contactsCertificatesTypeStr = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),
                patient.getContactsCertificatesType());
        //其他信息
        String provinceCodeStr = dictFeignClient.getName(patient.getProvinceCode());
        String cityCodeStr = dictFeignClient.getName(patient.getCityCode());
        String districtCodeStr = dictFeignClient.getName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeStr", certificatesTypeStr);
        patient.getParam().put("contactsCertificatesTypeStr", contactsCertificatesTypeStr);
        patient.getParam().put("provinceCodeStr", provinceCodeStr);
        patient.getParam().put("cityCodeStr", cityCodeStr);
        patient.getParam().put("districtCodeStr", districtCodeStr);
        return patient;
    }
}
